package com.sanweibook.jhook.web.sys;

import com.sanweibook.jhook.biz.sys.SysUserService;
import com.sanweibook.jhook.common.result.Result;
import com.sanweibook.jhook.shiro.hash.UserSimpleHash;
import com.sanweibook.jhook.shiro.util.UserAuthorizingRealmUtils;
import com.sanweibook.jhook.web.sys.vo.SysUserVO;
import com.sanweibook.jhook.web.util.UsernamePasswordTokenUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * Created by twg on 16/11/1.
 */
@Slf4j
@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private UserAuthorizingRealmUtils userAuthorizingRealmUtil;
    @Autowired
    private SysUserService sysUserService;


    /**
     * 用户登录首页
     *
     * @return
     */
    @RequestMapping(value = "login")
    public String login(HttpServletRequest request) {
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()){
            return "redirect:/admin/home.html";
        }
        return "login";
    }

    @RequestMapping(value = "login", method = RequestMethod.POST)
    @ResponseBody
    public Result<String> auth(@Valid SysUserVO user, BindingResult bindingResult) {
        List<FieldError> fieldErrors = bindingResult.getFieldErrors();
        for (FieldError fieldError : fieldErrors) {
            return Result.returnErrorResult("",fieldError.getDefaultMessage());
        }
        String userName = user.getUserName();
        String password = user.getUserPassword();
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(userName, password);
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(usernamePasswordToken);
            return Result.returnSuccessResult("用户登录成功");
        } catch (IncorrectCredentialsException credentialException) {
            log.error("username:{},password:{},错误的凭证",userName,password);
        } catch (UnknownAccountException unknownAccountException) {
            log.error("username:{},password:{},错误的帐号",userName,password);
        } catch (AuthenticationException authenticationException){
            log.error("username:{},password:{},错误的帐号",userName,password);
        }
        return Result.returnErrorResult("","用户登录失败");
    }

    @RequestMapping("chang-password")
    public String changPassword() {
        UserSimpleHash simpleHash = UsernamePasswordTokenUtils.doPassword("twg", "twg123456");
        //TODO 操作数据库，更新用户密码信息
        userAuthorizingRealmUtil.clearCachedAuthenticationInfo("twg");
        return "redirect:/admin/home.html";
    }
}
